import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;

public class clientSocketChat {
	Socket clientsocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;
	
	public clientSocketChat(){}
	public void run(){
		try{
			clientsocket = new Socket("bill.cs.hendrix.edu",8888);
			System.out.println("connected at local host on port 8888");
			out = new ObjectOutputStream(clientsocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(clientsocket.getInputStream());
			
			do{
				try{
					message = (String)in.readObject();
					System.out.println("server>" + message);
					sendMessage("Hi my server");
					message = "bye";
					sendMessage(message);
				}
				catch(ClassNotFoundException classNot){
					System.err.println("data received in unknown format");
				}
			}while(!message.equals("bye"));
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				in.close();
				out.close();
				clientsocket.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}
	
	public void sendMessage(String message){
		try{
			out.writeObject(message);
			out.flush();
			System.out.println("client >" + message);
			
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]){
		clientSocketChat client = new clientSocketChat();
		client.run();
	}
	
}
